import {setLocalStorage, clearLocalStorage} from "../utils";
import defaultState from './state';

//actions dispatch规则
interface ActionRule {
    type : string,
    data ?: any, //清除的时候不需要传data
}

function reducer(state = defaultState, action: ActionRule) {
    switch (action.type) {
        case 'setUserInfo':
            setLocalStorage('userInfo', action.data);
            return {
                ...state,
                userInfo: action.data
            };
        case 'setToken':
            setLocalStorage('access_token', action.data);
            return {
                ...state,
                access_token: action.data
            };
        case 'clearStore':
            clearLocalStorage();
            return defaultState;
        default :
            return state;
    }
}

export default reducer
